package com.lzy.mall.product.controller;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.product.entity.PmsBrand;
import com.lzy.mall.product.service.PmsBrandService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 品牌(PmsBrand)表控制层
 *
 * @author lzy
 * @since 2021-08-25 11:41:10
 */
@RestController
@RequestMapping("brand")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsBrandController {

    private final PmsBrandService pmsBrandService;

    /**
     * 品牌修改,及其关联
     * @param pmsBrand
     * @return
     */
    @PostMapping("/update")
    public ResultUtils brandUpdate(@RequestBody PmsBrand pmsBrand){
        pmsBrandService.brandUpdate(pmsBrand);
        return ResultUtils.ok("品牌修改成功");
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    public ResultUtils updateStatus(@RequestBody PmsBrand brand){
         pmsBrandService.updateStatus(brand);
        return ResultUtils.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public ResultUtils save(@RequestBody PmsBrand pmsBrand){
        pmsBrandService.save(pmsBrand);
        return ResultUtils.ok("品牌新增成功");
    }

    /**
     * 获取单个品牌
     * @param brandId
     * @return
     */
    @GetMapping("/info/{brandId}")
    public ResultUtils info(@PathVariable("brandId") Long brandId){
        PmsBrand brand = pmsBrandService.getById(brandId);

        return ResultUtils.ok().put("brand", brand);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public ResultUtils list(@RequestParam Map<String, Object> params){
        PageUtils pageQuery = pmsBrandService.queryPage(params);

        return ResultUtils.ok().put("page",pageQuery);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public ResultUtils delete(@RequestBody Long[] brandIds){
        pmsBrandService.removeByIds(CollUtil.toList(brandIds));
        return ResultUtils.ok();
    }

    @GetMapping("/infos")
    public ResultUtils info(@RequestParam("brandIds") List<Long> brandIds) {
        List<PmsBrand> brand = pmsBrandService.getBrandByIds(brandIds);
        return ResultUtils.ok().put("data", brand);
    }
}
